///
//  Animatem v 1.0
//  Russell Lowke, January 31st 2011
//
//  Copyright (c) 2011 Lowke Media
//  see http://www.lowkemedia.com for more information
//
//  Permission is hereby granted, free of charge, to any person obtaining a 
//  copy of this software and associated documentation files (the "Software"), 
//  to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//  and/or sell copies of the Software, and to permit persons to whom the 
//  Software is furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in 
//  all copies or substantial portions of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
//  IN THE SOFTWARE. 
//
//

// Starting to convert my Animatem AS3 package to JavaScript

var Animatem = function() 
{
    this.updateTime = new Date().getTime();         // (uint) time of last update
    this.timePassed = 0;                            // (uint) time passed since update
    this.animeList = new Array();                   // (array) list array of anime for iteration

    this.anime = function(target) 
    {
        if (! target) {
            throw new Error("anime() recieved a null target./r");
        }
        
        // try to get the anime
        var anime = this.getAnime(target);
        
        // if anime not found...
        if (! anime) {
            
            // ...then create a new anime
            anime = new Anime(this, target);
            this.animeList.push(anime);
        }
        
        return anime;
    }
    
    this.getAnime = function(target) 
    {
        var index = this.animeList.indexOf(target);
        if (index !== -1) {
            return this.animeList[index];
        }
        return;
    }
    
    this.update = function() 
    {
        var updateTime = new Date().getTime();
        this.timePassed = updateTime - this.updateTime;
        this.updateTime = updateTime;

        for (var i = 0; i < this.animeList.length; ++i) {
            this.animeList[i].update(this.updateTime);
        }
        
        // dispatchEvent(new Event(UPDATE));
        
    }
    
    this.remove = function() 
    {
        var anime = this.getAnime(target);
        if (anime) {
            anime.remove();
        }
    }
    
    this.clear = function() 
    {
        for (var i = 0; i < this.animeList.length; ++i) {
            remove(this.animeList[i].target);
        }
    }
    
    
    this.pause = function() 
    {
        for (var i = 0; i < this.animeList.length; ++i) {
            this.animeList[i].pause();
        }
    }
    
    this.unpause = function() 
    {
        for (var i = 0; i < this.animeList.length; ++i) {
            this.animeList[i].pause();
        }
    }
    
    
    /* WORRY ABOUT toString LATER
    this.dump = function dump() 
    {
        var str = "";
        
        // iterate through each anime
        for each (var i:Anime in _animeVect) {
            str += i;
        }
        for (var i = 0; i < this.animeList.length; ++i) {
            str += animeList[i];
        }

        return str; 
    }
    
    this.toString = function () {
        return dump();
    }
    */
}


// error, warning and ifo IDs
Animatem.WARNING_CANT_FIND_EFFECT               = 3000;
Animatem.WARNING_CANT_REMOVE_EFFECT             = 3001;
// Animatem.WARNING_CANT_FIND_STAGE             = 3002;
// Animatem.WARNING_SNAP_TO_END_NOT_SUPPORTED   = 3003;
// Animatem.WARNING_CYCLE_NOT_SUPPORTED         = 3004;
// Animatem.WARNING_REVERSE_NOT_SUPPORTED       = 3005;
// Animatem.WARNING_PAUSE_NOT_SUPPORTED         = 3006;
// Animatem.WARNING_UNPAUSE_NOT_SUPPORTED       = 3007;
// Animatem.DEBUG_REMOVED_FROM_STAGE            = 3008;
Animatem.DEBUG_EFFECT_BEING_REPLACED            = 3009;

        
//
// static helper methods

Animatem.xyToAngle = function(x, y) 
{   
    if (x === 0) {                  // avoid error due to divide by zero.
        if (y > 0) { 
            return 90;                  //  down
        } else { 
            return 270;                 //  up
        }
        
    } else {                        // quadrants

        var degrees = Animatem.radiansToDegrees(Math.atan(y/x));
        if (x > 0) {
            if (degrees < 0) { 
                return 360 + degrees; 
            } else { 
                return degrees; 
            } 
        } else { 
            return 180 + degrees;
        }
    }
}
    
///
// Convert radians to degrees.
// degrees = radians*180/PI
//
Animatem.radiansToDegrees = function(numRad) {
    // precalculate 180/PI = 57.295779513082289
    return numRad*57.295779513082289;
}
    
///
// Convert degrees to radians.
// radians = degrees*PI/180
//
Animatem.degreesToRadians = function(numDeg) {
    // precalculate PI/180 = 0.01745329251994
    return numDeg*0.01745329251994;
}